package cn.jja8.knapsackToGo4.bukkit.config.playerData;

import cn.jja8.config.tool.YamlConfig;
import cn.jja8.knapsackToGo4.all.work.Conf;
import cn.jja8.knapsackToGo4.all.work.PlayerDataCase;
import cn.jja8.knapsackToGo4.all.work.playerDataCase.file.FileDataCase;
import cn.jja8.knapsackToGo4.all.work.playerDataCase.file.FileDataCaseSetUp;
import cn.jja8.knapsackToGo4.all.work.playerDataCase.mysql.MysqlDataCase;
import cn.jja8.knapsackToGo4.all.work.playerDataCase.mysql.MysqlDataCaseSetUp;
import cn.jja8.knapsackToGo4.all.work.playerDataCase.sqlite.SqliteDataCase;
import cn.jja8.knapsackToGo4.all.work.playerDataCase.sqlite.SqliteDataCaseSetUp;
import cn.jja8.knapsackToGo4.bukkit.KnapsackToGo4;
import cn.jja8.knapsackToGo4.bukkit.error.DataCaseLoadError;
import cn.jja8.knapsackToGo4.bukkit.work.BukkitLogger;
import cn.jja8.knapsackToGo4.bukkit.work.BukkitTaskManager;

import java.io.File;

public enum PlayerDataCaseType {
    File,
    Sqlite,
    Mysql;


    public PlayerDataCase getDataCase() throws DataCaseLoadError {
        try{
            switch (this){
                default:return null;
                case File:{
                    FileDataCaseSetUp setUp = new FileDataCaseSetUp();
                    File config = new File(KnapsackToGo4.knapsackToGo4.getDataFolder(),"FileDataCaseSetUp.yml");
                    new YamlConfig().setAnnotationClass(Conf.class).load(config).as(setUp).save(config);
                    return new FileDataCase(setUp,BukkitLogger.get());
                }
                case Sqlite:{
                    SqliteDataCaseSetUp setUp = new SqliteDataCaseSetUp();
                    File config = new File(KnapsackToGo4.knapsackToGo4.getDataFolder(),"SqliteDataCaseSetUp.yml");
                    new YamlConfig().setAnnotationClass(Conf.class).load(config).as(setUp).save(config);
                    return new SqliteDataCase(setUp, new BukkitTaskManager(KnapsackToGo4.knapsackToGo4), BukkitLogger.get());
                }
                case Mysql:{
                    MysqlDataCaseSetUp setUp = new MysqlDataCaseSetUp();
                    File config = new File(KnapsackToGo4.knapsackToGo4.getDataFolder(),"MysqlDataCaseSetUp.yml");
                    new YamlConfig().setAnnotationClass(Conf.class).load(config).as(setUp).save(config);
                    return new MysqlDataCase(setUp, new BukkitTaskManager(KnapsackToGo4.knapsackToGo4), BukkitLogger.get());
                }
            }
        }catch (Error|Exception e){
            throw new DataCaseLoadError(e,"数据容器加载出错！");
        }

    }
}
